﻿namespace PMS.WebApi.Utils
{
    public class LogHelper
    {
        static object _locker = new object();

        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="tag">传入标签（这里用于标识函数由哪个线程调用）</param>
        //public static void WriteLog(Exception ex, string tag = "")
        //{
        //    lock (_locker)
        //    {
        //        string logDir = HttpContext.Current.Server.MapPath("/Logs/Exception");
        //        if (!Directory.Exists(logDir))
        //            Directory.CreateDirectory(logDir);
        //        string logFile = Path.Combine(logDir, DateTime.Now.ToString("yyyyMMdd") + ".txt");
        //        StreamWriter sw = new StreamWriter(logFile, true);
        //        sw.WriteLine(String.Concat('[', DateTime.Now.ToString("yyyy-MM-dd"), "] Tag:" + tag));
        //        sw.WriteLine("异常信息：" + ex.Message);
        //        sw.WriteLine("异常对象：" + ex.Source);
        //        sw.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
        //        sw.WriteLine("触发方法：" + ex.TargetSite);
        //        sw.WriteLine();
        //        sw.Close();
        //    }
        //}
    }
}
